package com.honghe.system.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.honghe.system.mapper.SysCaseAssignMapper;
import com.honghe.system.domain.SysCaseAssign;
import com.honghe.system.service.ISysCaseAssignService;
import com.honghe.system.service.ISysUserService;
import com.honghe.common.core.domain.entity.SysUser;

/**
 * 案件分配关系Service业务层处理
 */
@Service
public class SysCaseAssignServiceImpl implements ISysCaseAssignService
{
    @Autowired
    private SysCaseAssignMapper sysCaseAssignMapper;

    @Autowired
    private ISysUserService userService;

    /**
     * 查询案件分配关系列表
     * 
     * @param caseId 案件ID
     * @return 案件分配关系集合
     */
    @Override
    public List<SysCaseAssign> selectCaseAssignList(Long caseId)
    {
        return sysCaseAssignMapper.selectCaseAssignList(caseId);
    }

    /**
     * 新增案件分配关系
     * 
     * @param sysCaseAssign 案件分配关系
     * @return 结果
     */
    @Override
    public int insertCaseAssign(SysCaseAssign sysCaseAssign)
    {
        return sysCaseAssignMapper.insertCaseAssign(sysCaseAssign);
    }

    /**
     * 删除案件分配关系
     * 
     * @param caseId 案件ID
     * @return 结果
     */
    @Override
    public int deleteCaseAssignByCaseId(Long caseId)
    {
        return sysCaseAssignMapper.deleteCaseAssignByCaseId(caseId);
    }

    /**
     * 批量删除案件分配关系
     * 
     * @param caseIds 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteCaseAssignByCaseIds(Long[] caseIds)
    {
        return sysCaseAssignMapper.deleteCaseAssignByCaseIds(caseIds);
    }

    /**
     * 分配案件
     *
     * @param caseId 案件ID
     * @param assignedTo 被分配用户ID
     * @return 结果
     */
    @Override
    @Transactional
    public int assignCase(Long caseId, Long assignedTo)
    {
        // 先删除原有的分配关系
        deleteCaseAssignByCaseId(caseId);
        
        // 创建新的分配关系
        SysCaseAssign caseAssign = new SysCaseAssign();
        caseAssign.setCaseId(caseId);
        caseAssign.setAssignedTo(assignedTo);
        
        // 获取用户信息
        SysUser user = userService.selectUserById(assignedTo);
        if (user != null)
        {
            caseAssign.setAssignedToName(user.getNickName());
        }
        
        return insertCaseAssign(caseAssign);
    }

    /**
     * 获取案件分配信息
     *
     * @param caseId 案件ID
     * @return 分配信息
     */
    @Override
    public SysCaseAssign selectCaseAssignByCaseId(Long caseId)
    {
        List<SysCaseAssign> assigns = selectCaseAssignList(caseId);
        return assigns != null && !assigns.isEmpty() ? assigns.get(0) : null;
    }
} 